<?php

namespace app\common\model;

use app\admin\model\ProjectFeeitem;
use app\common\model\apply\Advance;
use app\common\model\apply\Payment;
use app\common\model\apply\Reimbursement;
use app\common\model\routine\Feeitem;
use app\common\model\workflow\Node;
use app\common\model\workflow\NodeRecord;
use app\common\model\workflow\NodeScope;
use app\common\model\workflow\Workflow;
use think\facade\Log;
use think\Model;

/**
 * Project
 */
class Project extends Model
{
    // 表名
    protected $name = 'project';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 追加属性
    protected $append = [
        'member', 'manager'
    ];


    public function getMemberAttr($value, $row): array
    {
        return [
            'nickname' => \app\admin\model\Admin::whereIn('id', $row['member_ids'])->column('nickname'),
        ];
    }

    public function getMemberIdsAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setMemberIdsAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getSubTotalAttr($value): float
    {
        return (float)$value;
    }

    public function getTaxAttr($value): float
    {
        return (float)$value;
    }

    public function getTotalAttr($value): float
    {
        return (float)$value;
    }

    public function manager(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\Admin::class, 'manager_id', 'id');
    }

    public static function onAfterInsert(Model $model): void
    {
        $feeitems = Feeitem::select()->toArray();
        $topFeeitems = [];
        foreach ($feeitems as $v){
            if($v['pid'] == 0) $topFeeitems[$v['id']] = $v['title'];
        }
        $saveData = [];
        foreach ($feeitems as $v){
            if($v['pid'] > 0) {
                $saveData[] = [
                    'project_id' => $model->id,
                    'feeitem_id' => $v['id'],
                    'feeitem_name' => $v['title'],
                    'feeitem_pid' => $v['pid'],
                    'feeitem_pname' => $topFeeitems[$v['pid']],
                ];
            }
        }
        (new ProjectFeeitem())->saveAll($saveData);
    }

    public static function onAfterDelete(Model $model): void
    {
        // 清除 申请以及对应的审批
        $applys = Reimbursement::where(['project_id' => $model->id])->field('wokerflow_id')->select()->toArray();
        $wokerflow_ids = [];
        foreach ($applys as $v){
            $wokerflow_ids[] = $v['wokerflow_id'];
        }
        $applys = Payment::where(['project_id' => $model->id])->field('wokerflow_id')->select()->toArray();
        foreach ($applys as $v){
            $wokerflow_ids[] = $v['wokerflow_id'];
        }
        $applys = Advance::where(['project_id' => $model->id])->field('wokerflow_id')->select()->toArray();
        foreach ($applys as $v){
            $wokerflow_ids[] = $v['wokerflow_id'];
        }
        Workflow::where([
            ['id', 'in', $wokerflow_ids]
        ])->delete();
        Workflow::where([
            ['extend' => $model->id]
        ])->delete();
        Node::where([
            ['flow_id', 'in', $wokerflow_ids]
        ])->delete();
        NodeRecord::where([
            ['flow_id', 'in', $wokerflow_ids]
        ])->delete();
        NodeScope::where([
            ['flow_id', 'in', $wokerflow_ids]
        ])->delete();
        Reimbursement::where(['project_id' => $model->id])->delete();
        Payment::where(['project_id' => $model->id])->delete();
        Advance::where(['project_id' => $model->id])->delete();
    }
}